// pages/market/user/user-address.js
import api from "../../../utils/api.js";
import market_cart from '../../../utils/market/market_cart'
import message from "../../../utils/message.js";


Page({

    /**
     * 页面的初始数据
     */
    data: {

        user_address_list: [],
        has_load: false

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getUserAddressList();

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getUserAddressList:function(){
        var me = this;

        message.show_loading();
        market_cart.getUserMarketAddressList( {} , function( response ){
            message.hide_loading();

            if(response.status==1){
                me.setData({
                    user_address_list:response.data,
                    has_load:true
                })

            }else{
                message.show_error(response.message)
            }

        } )

    },
    removeAddress:function(e){
        var id = e.currentTarget.dataset.id;
        var me = this;

        message.confirm("确定删除当前收货地址?", "删除" , "取消" , function (index) {

            if(index==1){

                message.show_loading("正在删除中");
                market_cart.deleteUserAddress( {address_id: id } , function(response){
                    message.hide_loading();
                    if(response.status==1){
                        message.show_error( response.message )
                    }

                    me.getUserAddressList();

                })
            }
            
        })
        

    },
    editAddress:function(e){

        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/market/address/add_user_address?type=edit&id='+id
        })
    },
    useAddress:function(e){
        var index = e.currentTarget.dataset.index;


        var address = this.data.user_address_list[index];

        var pages = getCurrentPages();

        var len = pages.length;

        var prepage = pages[len-2];
        if(address){
            prepage.setUserAddress( address );

            wx.navigateBack({
              delta: 1,
            })
        }

    },
    setDefaultAddress:function (e) {
        var me = this;
        var id = e.currentTarget.dataset.id;

        var params = {};
        params.id = id;
        api.shop.setUserDefalutAddress( params , function ( response ) {
            if(response.status==1){
                message.show_success( response.message  , function () {
                    me.getUserAddressList();
                    
                })
            }else{
                message.show_error( response );
            }
        })
        
    }
})